//
//  DetailWebView.m
//  iNews
//
//  Created by lanou3g on 14-12-10.
//  Copyright (c) 2014年 张在青. All rights reserved.
//

#import "DetailWebView.h"
#import "DetailModel.h"

@implementation DetailWebView

- (void)dealloc
{
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
      //  self.backgroundColor = [UIColor yellowColor];
        
        
        
    }
    return self;
}

-(void)getThml:(DetailModel *)detail
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"html" ofType:@"txt"];
    NSMutableString *str = [NSMutableString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    
    NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"htmlCSS" ofType:@"css"];
    
    NSRange rangeOfCSSPath = [str rangeOfString:@"#CSSPath#"];
    
    NSLog(@"%lu",(unsigned long)rangeOfCSSPath.length);
    
    NSLog(@"%lu",(unsigned long)rangeOfCSSPath.location);
    
    
    [str replaceCharactersInRange:rangeOfCSSPath withString:cssPath];
    //取子串在父串中的位置及范围
    NSRange titleRange = [str rangeOfString:@"#title#"];
    [str replaceCharactersInRange:titleRange withString:detail.title];
    
    NSRange sourceRange = [str rangeOfString:@"#source#"];
    [str replaceCharactersInRange:sourceRange withString:detail.source];
    
    NSRange timeRange = [str rangeOfString:@"#time#"];
    [str replaceCharactersInRange:timeRange withString:detail.ptime];
    
    NSRange bodyRange = [str rangeOfString:@"#body#"];
    [str replaceCharactersInRange:bodyRange withString:detail.body];
    
    for (NSDictionary *dict in detail.img) {
        
        int a;
        int b;
        if (dict[@"pixel"] != nil) {
            NSArray *arr = [dict[@"pixel"] componentsSeparatedByString:@"*"];
            a = [arr[0] intValue];
            b = [arr[1] intValue];
        }else{
            a = 1;
            b = 1;
        }
        
        NSMutableString *imgHtml = [NSMutableString stringWithFormat:@"<img class=\"content-image\" src=\"%@\" alt=\"\" width = 305px height = %@px /><p style=\"font-size:14px\">%@</p>",[dict objectForKey:@"src"],[NSString stringWithFormat:@"%d",305*b/a],[dict objectForKey:@"alt"]];
        
        
        NSRange rangeOfImg = [str rangeOfString:[dict objectForKey:@"ref"]];
        
        if (rangeOfImg.length != 0) {
            
            [str replaceCharactersInRange:rangeOfImg withString:imgHtml];
        }
        
        
    }
    
    [self loadHTMLString:str baseURL:nil];
}








/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
